#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

//erase(iter) insert(begin(), iter)
vector<int> moveElementsToBegin(vector<int> array, int target) {
    vector<int> array1;
    for(auto iter = array.end() - 1; iter >= array.begin(); --iter){
        if(*iter == target){
            array1.insert(array1.begin(), target);
        }
        else{
            array1.insert(array1.end(), *iter);
        }
    }

    return array1;
}

vector<int> moveElementsToBegin1(vector<int> array, int target){
    int x = 0;
    int y = array.size() - 1;

    while(y > x){
        if(array[x] == target){
            ++x;
        }
        
        if(array[y] == target){
            swap(array[x], array[y]);
        }
        else{
            --y;
        }
    }
    return array;
}

int main(int argc, char const *argv[])
{
    vector<int> array{4, 1, 3, 2, 2, 2, 2, 2};
    int target = 2;
    vector<int> array1 = moveElementsToBegin1(array, target);
    for(int i = 0; i < array1.size(); ++i){
        cout << array1[i];
    }
    cout << endl;
    return 0;
}
